<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="86px">
      <el-form-item label="APP名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入APP名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="版本" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入版本"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:appInfo:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:appInfo:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:appInfo:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-popover
          placement="bottom"
          width="550"
          trigger="click">
          <div v-if="QRImage" style="text-align: center">
            <div>下载地址： {{ qrUrl }}</div>
            <el-image :src="QRImage" style="width: 250px;height:250px"/>
          </div>
          <el-button
            type="primary"
            plain
            size="mini"
            slot="reference"
          >
            <svg-icon icon-class="qrcode" class="el-input__icon input-icon"/>
            查看二维码
          </el-button>
        </el-popover>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id" v-if="false"/>
      <el-table-column label="APP名称" align="center" prop="name" width="250px"/>
      <el-table-column label="版本" align="center" prop="version" width="100px"/>
      <el-table-column label="文件路径" align="center" prop="filePath">
        <template slot-scope="{row}">
          <el-link :href="baseUrl+row.downloadPath" type="primary">{{ row.filePath }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="CDN路径" align="center" prop="cdnPath">
        <template slot-scope="{row}">
          <el-link :href="row.cdnPath" type="primary">{{ row.cdnPath }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding" fixed="right" width="125px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:appInfo:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:appInfo:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改APP版本管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="small">
        <el-form-item label="APP名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入APP名称"/>
        </el-form-item>
        <el-form-item label="版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本"/>
        </el-form-item>
        <el-form-item label="APP编号" prop="appCode">
          <el-select
            v-model="form.appCode"
            placeholder="APP编号"
            clearable
            size="small"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.sys_app_code"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="CDN路径" prop="cdnPath">
          <el-input v-model="form.cdnPath" placeholder="CDN路径"/>
        </el-form-item>
        <el-form-item label="文件路径">
          <!--<FileUpload :file-type="['apk']" v-model="form.filePath" :limit="1" :file-size="20"/>-->
          <AttachmentUpload
            :file-type="['apk']"
            value-type="path"
            v-model="form.filePath"
            :file-size="20"
            :delete-file-on-remove="false"
            remark="APP管理"
            business-type="system"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listAppInfo, getAppInfo, delAppInfo, addAppInfo, updateAppInfo, exportAppInfo} from "@/api/system/appInfo";
import QRCode from 'qrcode';

export default {
  name: "AppInfo",
  dicts: ['sys_app_code'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      QRImage: null,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // APP版本管理表格数据
      appInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        version: undefined,
        filePath: undefined,
        appCode: undefined,
        cdnPath: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "APP名称不能为空", trigger: "blur"}
        ],
        appCode: [
          {required: true, message: "app编号", trigger: "change"}
        ],
        version: [
          {required: true, message: "版本不能为空", trigger: "blur"}
        ],
        filePath: [
          {required: true, message: "文件路径不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    //生成二维码
    QRCode.toDataURL(this.qrUrl)
      .then(url => {
        this.QRImage = url;
      })
      .catch(err => {
      })
  },
  computed: {
    qrUrl() {
      return location.origin + this.baseUrl + '/system/appInfo/updateApp'
    }
  },
  methods: {
    /** 查询APP版本管理列表 */
    getList() {
      this.loading = true;
      listAppInfo(this.queryParams).then(response => {
        this.appInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        version: undefined,
        filePath: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        appCode: '0',
        cdnPath: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加APP版本管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getAppInfo(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改APP版本管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateAppInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addAppInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除APP版本管理编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delAppInfo(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有APP版本管理数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportAppInfo(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
